package com.zlq.ajaxjson.controller;

import com.zlq.ajaxjson.dao.UserDaoLo;
import com.zlq.ajaxjson.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@WebServlet("/fenYeController")
public class FenYeController extends HttpServlet {
    private final UserDaoLo ud = new UserDaoLo();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        String rows = req.getParameter("rows");
        /*System.out.println();
        System.out.println();*/
        /*List<User> ll = new LinkedList<>();
        ll.add(new User(1,"zz", "123"));
        ll.add(new User(2,"xx", "123"));
        ll.add(new User(3,"yy", "123"));
        ll.add(new User(4,"ww", "123"));
        ll.add(new User(5,"uu", "123"));
        ll.add(new User(6,"pp", "123"));*/
        List<User> ll = null;
        try {
            ll = ud.selectPage(Integer.parseInt(page), Integer.parseInt(rows));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows", ll);
        map.put("total", ud.count());
        PrintWriter out = resp.getWriter();
        out.write(JSONObject.fromObject(map).toString());
        out.flush();
        out.close();
    }
}
